﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Data.Sql;
using System.Data.SqlClient;
using YouHP.Model;
using Microsoft.ApplicationBlocks.Data;

namespace YouHP.Atom
{
    public class PatientTreatmentHistories
    {
        static readonly string _sGetHistoriesByPrId = "GetTreatmentHistoriesByPrdId";
        static readonly string _sGetHistoryById = "GetTreatmentHistoryByHistoryId";
        static readonly string _sGetHistoriesByTreatmentId = "GetTreatmentHistoriesByTreatmentId";
        static readonly string _sGetHistoriesByTreatDate = "GetTreatmentHistoriesByTreatmentDate";
        static readonly string _sGetTreatmentByPrdId = "GetTreatmentsByPrdId";
        static readonly string _sAddHistory = "AddTreatmentHistory";
        static readonly string _sUpdateHistory = "UpdateTreatmentHistory";
        static readonly string _sDeleteHistory = "DeleteTreatmentHistory";
        static readonly string _sDeleteHistories = "DeleteTreatmentHistories";
        static readonly string _sUpdateCurrentTreatment = "UpdateCurrentTreatment";
        static readonly string _sGetCurrentTreatment = "GetCurrentTreatment";

        public TreatmentHistoryInfo GetHistoryById( int iHistoryId)
        {
            return CBO.FillObject<TreatmentHistoryInfo>(SqlHelper.ExecuteReader(Common.ConnStr, _sGetHistoryById, iHistoryId));
        }
        
        public List<TreatmentHistoryInfo> GetHistoriesByTreatmentId(int prId, int iTreatmentId, int iPageSize, int iPageNo, out int iCount)
        {
            DataSet ds = SqlHelper.ExecuteDataset(Common.ConnStr,
               _sGetHistoriesByTreatmentId, prId, iTreatmentId, iPageSize, iPageNo);
            IDataReader dr = ds.Tables[0].CreateDataReader();
            if (dr != null && dr.Read())
            {
                iCount = dr.GetInt32(0);
                dr.Close();
            }
            else
            {
                iCount = 0;
            }
            dr = ds.Tables[1].CreateDataReader();
            return CBO.FillCollection<TreatmentHistoryInfo>(dr);
        }

        public List<TreatmentHistoryInfo> GetHistoriesByPrId(int prId, int iPageSize, int iPageNo, out int iCount)
        {
            DataSet ds = SqlHelper.ExecuteDataset(Common.ConnStr,
               _sGetHistoriesByPrId, prId, iPageSize, iPageNo);
            IDataReader dr = ds.Tables[0].CreateDataReader();
            if (dr != null && dr.Read())
            {
                iCount = dr.GetInt32(0);
                dr.Close();
            }
            else
            {
                iCount = 0;
            }
            dr = ds.Tables[1].CreateDataReader();
            return CBO.FillCollection<TreatmentHistoryInfo>(dr);
        }

        public List<TreatmentHistoryInfo> GetHistoriesByTreatmentId(int prId, int iTreatmentId, DateTime dStart, DateTime dEnd, int iPageSize, int iPageNo, out int iCount)
        {
            return GetHistoriesByTreatmentId(prId, iTreatmentId, dStart, dEnd, iPageSize, iPageNo, 1, out iCount);
        }

        public List<TreatmentHistoryInfo> GetHistoriesByTreatmentId(int prId, int iTreatmentId, DateTime dStart, DateTime dEnd, int iPageSize, int iPageNo, int asc, out int iCount)
        {
            DataSet ds = SqlHelper.ExecuteDataset(Common.ConnStr,
               _sGetHistoriesByTreatDate, prId, iTreatmentId, dStart, dEnd, iPageSize, iPageNo, asc);
            IDataReader dr = ds.Tables[0].CreateDataReader();
            if (dr != null && dr.Read())
            {
                iCount = dr.GetInt32(0);
                dr.Close();
            }
            else
            {
                iCount = 0;
            }
            dr = ds.Tables[1].CreateDataReader();
            return CBO.FillCollection<TreatmentHistoryInfo>(dr);
        }

        public List<TreatmentHistoryInfo> GetTreatmentsByPrId(int prdId)
        {
            return CBO.FillCollection<TreatmentHistoryInfo>(SqlHelper.ExecuteReader(Common.ConnStr, _sGetTreatmentByPrdId, prdId));
        }

        public int AddRecord(TreatmentHistoryInfo info)
        {
            SqlParameter[] outParms = new SqlParameter[]  {
                new SqlParameter("@PRId", SqlDbType.Int),
                new SqlParameter("@TreatmentId", SqlDbType.Int),
                new SqlParameter("@DataValue", SqlDbType.Float),
                new SqlParameter("@Frequency", SqlDbType.NVarChar, 50),
                new SqlParameter("@State", SqlDbType.Int),
                new SqlParameter("@StartDate", SqlDbType.DateTime),
                new SqlParameter("@EndDate", SqlDbType.DateTime),
                new SqlParameter("@StopReason", SqlDbType.Int),
                new SqlParameter("@UsePurpose", SqlDbType.Int),
                new SqlParameter("@UpdateDate", SqlDbType.DateTime),
                new SqlParameter("@Privacy", SqlDbType.Int),
                new SqlParameter("@Id", SqlDbType.Int)
            };
            outParms[0].Value = info.PRId;
            outParms[1].Value = info.TreatmentId;
            outParms[2].Value = info.DataValue;
            outParms[3].Value = info.Frequency;
            outParms[4].Value = info.State;
            outParms[5].Value = info.StartDate;
            outParms[6].Value = info.EndDate;
            outParms[7].Value = info.StopReason;
            outParms[8].Value = info.UsePurpose;
            outParms[9].Value = info.UpdateDate;
            outParms[10].Value = info.Privacy;
            outParms[11].Direction = ParameterDirection.Output;

            SqlHelper.ExecuteNonQuery(Common.ConnStr, CommandType.StoredProcedure, _sAddHistory, outParms);

            info.Id =  Convert.ToInt32(outParms[11].Value.ToString());
            return info.Id > 0 ? 0 : -1; 
        }

        public int UpdateRecord(TreatmentHistoryInfo info)
        {
            try
            {
                SqlHelper.ExecuteNonQuery(Common.ConnStr,
                _sUpdateHistory,
                info.Id,
                info.PRId,
                info.TreatmentId,
                info.DataValue,
                info.Frequency,
                info.State,
                info.StartDate,
                info.EndDate,
                info.StopReason,
                info.UsePurpose,
                info.UpdateDate,
                info.Privacy
                );
                return 0;
            }
            catch (System.Exception e)
            {
                return -1;
            }
             
        }

        public int DeleteRecord(TreatmentHistoryInfo info)
        {
            try
            {
                SqlHelper.ExecuteNonQuery(Common.ConnStr, _sDeleteHistory, info.Id, info.PRId, info.TreatmentId); 
                return 0;
            }
            catch (System.Exception e)
            {
            	return -1;
            }
        }

        public int DeleteRecord(int PrId, int TreatmentId)
        {
            try
            {
                SqlHelper.ExecuteNonQuery(Common.ConnStr, _sDeleteHistories, PrId, TreatmentId);
                return 0;
            }
            catch (System.Exception e)
            {
                return -1;
            }
        }

        public int UpdateCurrentTreatment(TreatmentHistoryInfo info)
        {
            //try
            //{
                SqlHelper.ExecuteNonQuery(Common.ConnStr, _sUpdateCurrentTreatment, info.PRId, info.TreatmentId, info.IsUsing, info.DataValue, info.EndDate, info.Id);
                return 0 ;
            //}
            //catch (System.Exception e)
            //{
            //    return -1;	
            //}
          
        }

        public TreatmentHistoryInfo GetCurrentTreatment(int iPrId, int iTreatmentId)
        { 
            return CBO.FillObject<TreatmentHistoryInfo>(SqlHelper.ExecuteReader(Common.ConnStr, _sGetCurrentTreatment, iPrId, iTreatmentId));
        }
    }
}
